#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 30 avr. 2011

@author: alinehuf

UIElement includes classes managing the creation of the StartUp menu
and the different text boxes
'''

from PyQt4 import QtGui, QtCore

from ui.UiDimension import UiDimension #
uiD = UiDimension()

class UiMenu(QtGui.QWidget):
    def __init__(self, parent, texte, bouton1, bouton2):
        '''
        Creation of a dialog window with two button 
        and a ComboBox to choose the level
        '''
        super(UiMenu, self).__init__(parent)
        self.mainWindow = parent
        label = QtGui.QLabel(texte, self)
        label.resize(uiD.menuW, uiD.menuH)
        label.setAlignment(QtCore.Qt.AlignTop and QtCore.Qt.AlignHCenter)
        okButton = QtGui.QPushButton(bouton1)
        self.connect(okButton, QtCore.SIGNAL('clicked()'), parent._jouer)
        quitButton = QtGui.QPushButton(bouton2)
        self.connect(quitButton, QtCore.SIGNAL('clicked()'), parent.close)
        self.chooseLevel = QtGui.QComboBox()
        self.chooseLevel.addItem("niveau1 : combat de printemps")
        self.chooseLevel.addItem("niveau2 : combat d'hiver")
        self.connect(self.chooseLevel, QtCore.SIGNAL('activated(QString)'), \
                                                                self._setNiveau)
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(label)
        vbox.addWidget(self.chooseLevel)
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(okButton)
        hbox.addWidget(quitButton)
        vbox.addLayout(hbox)
        self.setLayout(vbox)
        x = (uiD.fenetreW - uiD.menuW) / 2
        y = (uiD.fenetreH - uiD.menuH) / 2
        self.setGeometry(x, y, uiD.menuW, uiD.menuH)
        
    def _setNiveau(self):
        '''
        Inform the mainWIndow of the level choose
        '''
        if self.chooseLevel.currentIndex() == 0 : 
            self.mainWindow.niveau = "niveau1"
        if self.chooseLevel.currentIndex() == 1 : 
            self.mainWindow.niveau = "niveau2"

class UiZone(QtGui.QWidget):
    def __init__(self, parent, titre, x, y, w, h):
        '''
        Creation of a widget with a title and an inner text
        '''
        super(UiZone, self).__init__(parent)
        self.setGeometry(x, y, w, h)
        self.fond = QtGui.QWidget(self)
        self.fond.resize(w, h)
        self.fond.setStyleSheet(uiD.styleZones)
        if titre : 
            self.titre = QtGui.QLabel(titre, self)
            self.titre.setStyleSheet(uiD.styleZonesTitre)
            self.texte  = QtGui.QLabel("", self)
            self.texte.setStyleSheet(uiD.styleZonesTexte)
            self.texte.resize(w - uiD.TexteMarginLeft, h)
            self.texte.setAlignment(QtCore.Qt.AlignTop)
    
    def changerTexte(self, texte):
        '''
        Change the text of the UiZOne
        '''
        self.texte.setText(texte)

class UiZoneEtiquette(UiZone):
    def __init__(self, parent, titre, etiquettes, x, y, w, h):
        '''
        Create a spécial UiZone with a set of "labels" in front of the
        text showing the corresponding values
        '''
        super(UiZoneEtiquette, self).__init__(parent, titre, x, y, w, h)
        self.etiquettes  = QtGui.QLabel(etiquettes, self)
        self.etiquettes.setStyleSheet(uiD.styleZonesEtiquettes)
        self.etiquettes.resize(uiD.EtiquettesWidth - uiD.TexteMarginLeft, h)
        self.etiquettes.setAlignment(QtCore.Qt.AlignTop)
        self.etiquettes.setVisible(False)
        self.texte.setStyleSheet(uiD.styleZonesTexte)
        self.texte.move(uiD.EtiquettesWidth - uiD.TexteMarginLeft, 0)

    def changerTexte(self, texte):
        '''
        Change the text values and shows the labels if the text is not null
        '''
        self.texte.setText(texte)
        if texte == "" : self.etiquettes.setVisible(False)
        else : self.etiquettes.setVisible(True)

class UiAction(UiZone):
    def __init__(self, parent, titre, x, y, w, h):
        '''
        Creation of the "action" as a special uiZone 
        with "active" labels instead of simple text
        '''
        super(UiAction, self).__init__(parent, None, x, y, w, h)
        self.titre = QtGui.QLabel(titre, self)
        self.titre.setStyleSheet(uiD.styleZonesTitre)
        self.actions  = []
        self.w = w
        self.mainWindow = parent
    
    def changerTexte(self, listeActions):
        '''
        Change the set of "active" labels representing the actions
        '''
        for oldAction in self.actions :
            oldAction.deleteLater()
        self.actions = []
        for x in range(len(listeActions)) :
            self.actions.append(UiActionLabel(self, self.mainWindow, \
                                              listeActions[x], \
                                              uiD.TexteMarginLeft, \
                                              uiD.TexteMarginTop * (x + 1), \
                                              self.w - uiD.TexteMarginLeft, \
                                              uiD.TexteHeight) )

class UiActionLabel(QtGui.QLabel):
    def __init__(self, parent, mainWindow, texte, x, y, w, h):
        '''
        a label representing an action
        '''
        super(UiActionLabel, self).__init__(parent)
        self.setText(texte)
        self.setGeometry(x, y, w, h)
        self.setVisible(True)
        self.mainWindow = mainWindow
    
    def mousePressEvent(self, mouseEvent):
        '''
        inform the "buisiness" of the chosen action
        '''
        self.mainWindow.controleur.demandeCompetence(self.text())

    def enterEvent(self, mouseEvent):
        '''
        roll-over effect
        '''
        self.setStyleSheet("QLabel { font-weight: bold; }")
        
    def leaveEvent(self, mouseEvent):
        '''
        roll-over effect
        '''
        self.setStyleSheet("QLabel { font-weight: normal; }")
